//
//  WKWalletViewController.m
//  ourToken
//
//  Created by wakeup on 2018/5/29.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKWalletViewController.h"
#import "WKBaseHeader.h"

@interface WKWalletViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIImageView *backImageView;//背景
@property (nonatomic, strong) UIButton *portraitBtn;//头像按钮
@property (nonatomic, strong) UILabel *allMoneyLabel;//总金额
@property (nonatomic, strong) UIView *bottomView;//控制器下面的view
@property (nonatomic, strong) UITableView *tableView;//view上的tableView
@property (nonatomic, strong) NSMutableArray *walletArray;//数据源

@end

@implementation WKWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"WKWalletTableViewCell" bundle:nil] forCellReuseIdentifier:@"WalletCell"];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = @"钱包";
    self.navigationView.topTitleLabel.textColor = [UIColor whiteColor];
    self.navigationView.backgroundColor = [UIColor clearColor];
    self.navigationView.topLeftButton.hidden = YES;
    self.navigationView.topRightButton.hidden = NO;
    [self.navigationView.topRightButton setImage:[UIImage imageNamed:@"chuang"] forState:UIControlStateNormal];
    self.navigationView.topLineView.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self getWalletListNetworking];
}

- (void)layoutUI {
    [self.view addSubview:self.backImageView];
    [self.view addSubview:self.portraitBtn];
    [self.view addSubview:self.allMoneyLabel];
    [self.view addSubview:self.bottomView];
    [self setBottomViewCorner];
    [self.bottomView addSubview:self.tableView];
    [self.view bringSubviewToFront:self.navigationView];
}

//获取钱包列表
- (void)getWalletListNetworking {
    [httpManager getWalletListWithPageIndex:1 pageSize:10 success:^(NSDictionary *result) {
        
        [self.walletArray removeAllObjects];
        NSDictionary *data = [result objectForKey:@"data"];
        self.allMoneyLabel.text = [data objectForKey:@"totalBalance"];
        NSDictionary *wallets = [data objectForKey:@"wallets"];
        for (NSDictionary *dict in wallets) {
            WKWalletModel *list = [WKWalletModel mj_objectWithKeyValues:dict];
            [self.walletArray addObject:list];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.walletArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WKWalletTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WalletCell" forIndexPath:indexPath];
    WKWalletModel *walletModel = self.walletArray[indexPath.row];
    cell.walletModel = walletModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WKWalletHistoryViewController *walletHistory = [[WKWalletHistoryViewController alloc] init];
    walletHistory.walletModel = self.walletArray[indexPath.row];
    [self.navigationController pushViewController:walletHistory animated:YES];
}

#pragma mark - 懒加载
- (UIImageView *)backImageView {
    if (_backImageView == nil) {
        _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, AppManager.height-AppManager.tabBarHeight)];
        _backImageView.image = [UIImage imageNamed:@"shoubg1"];
    }
    return _backImageView;
}

- (UIButton *)portraitBtn {
    if (_portraitBtn == nil) {
        _portraitBtn = [[UIButton alloc] initWithFrame:CGRectMake((AppManager.width-100*AppManager.scale)/2, AppManager.navigationHeight+15*AppManager.scale, 100*AppManager.scale, 100*AppManager.scale)];
        [_portraitBtn setBackgroundImage:[UIImage imageNamed:@"logo"] forState:UIControlStateNormal];
        _portraitBtn.userInteractionEnabled = NO;
        _portraitBtn.layer.cornerRadius = (_portraitBtn.frame.size.width)/2;
        _portraitBtn.layer.masksToBounds = YES;
    }
    return _portraitBtn;
}

- (UILabel *)allMoneyLabel {
    if (_allMoneyLabel == nil) {
        _allMoneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.portraitBtn.frame)+15*AppManager.scale, AppManager.width, 30*AppManager.scale)];
        _allMoneyLabel.textColor = [UIColor whiteColor];
        _allMoneyLabel.textAlignment = NSTextAlignmentCenter;
        _allMoneyLabel.font = AppManager.tableTextFont;
        _allMoneyLabel.text = @"≈4454.33 (￥)";
    }
    return _allMoneyLabel;
}

- (UIView *)bottomView {
    if (_bottomView == nil) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, AppManager.height*0.45, AppManager.width, AppManager.height*0.55-AppManager.tabBarHeight)];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

- (NSMutableArray *)walletArray {
    if (_walletArray == nil) {
        _walletArray = [[NSMutableArray alloc]init];
    }
    return _walletArray;
}

//设置圆角
- (void)setBottomViewCorner {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bottomView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bottomView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.bottomView.layer.mask = maskLayer;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _bottomView.frame.size.width, _bottomView.frame.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (void)rightButtonClick:(UIButton *)sender {
//    WKScanCodeViewController *scanCode = [[WKScanCodeViewController alloc] init];
//    [self.navigationController pushViewController:scanCode animated:YES];
    
    WKFoundWalletViewController *foundWallet = [[WKFoundWalletViewController alloc] init];
   foundWallet.foundType=1;
    [self.navigationController pushViewController:foundWallet animated:YES];
}

@end
